package com.heima.juc.n8;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author : leimingchao
 * @date Date : 2022年02月19日 19:41
 */
@Slf4j(topic = "c.TestShutDown")
public class TestShutDown {

    public static void main(String[] args) throws InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(2);

        Future<Integer> result1 = threadPool.submit(() -> {
            log.debug("task 1 running ...");
            Thread.sleep(1000);
            log.debug("task 1 finish...");
            return 1;
        });


        Future<Integer> result2 = threadPool.submit(() -> {
            log.debug("task 2 running ...");
            Thread.sleep(1000);
            log.debug("task 2 finish...");
            return 2;
        });

        Future<Integer> result3 = threadPool.submit(() -> {
            log.debug("task 3 running ...");
            Thread.sleep(1000);
            log.debug("task 3 finish...");
            return 3;

        });

        log.debug("shutdown");
        // threadPool.shutdown();
        //不建议使用，因为不知道要等待多久
        //threadPool.awaitTermination(3, TimeUnit.SECONDS);

        //返回未执行的任务
        List<Runnable> runnables = threadPool.shutdownNow();

        log.debug("other....{}", runnables);
    }
}
